﻿using System.Windows;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 使子面板尝试“半自动编号”。
        /// 
        /// 一旦子面板中出现标题文本块或有任何一个文本块的左缩进值大于零，则会放弃尝试。
        /// </summary>
        public static void SwitchTrySemiAutoNum()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null)
            {
                Globals.selectedEditor.SelectedExpression = Globals.selectedEditor.RootPanel;
            }

            LunarPresentation.Expressions.SubPanel curSubPanel =
                Globals.selectedEditor.SelectedExpression as Expressions.SubPanel;

            if (curSubPanel == null)
            {
                curSubPanel = Globals.selectedEditor.SelectedExpression.ParentPanel;

                if (curSubPanel == null)//如果父面板也为空。
                {
                    MessageBox.Show("请选中一个子面板！", Globals.appName,
                           MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }

            if (curSubPanel.Direction == Orientation.Horizontal)
            {
                MessageBox.Show("目标子面板是“水平”指向的，“开关‘半自动编号’”这个操作没有意义！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            bool openSemiAutoNum = !curSubPanel.TrySemiAutoNum;

            ModifingItem mi = new ModifingItem();

            Action act = new Action(curSubPanel.ExID, -1, curSubPanel.GetType().Name,
                "TrySemiAutoNum", curSubPanel.TrySemiAutoNum.ToString(), openSemiAutoNum.ToString());

            curSubPanel.TrySemiAutoNum = openSemiAutoNum;

            mi.Add(act);
            curSubPanel.Editor.Do(mi);
            curSubPanel.Editor.IsModified = true;
        }
    }
}
